import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
    getCrawlerArticles,
    deleteCrawlerArticle,
    publishCrawlerArticle,
    updateArticlesSource,
    updateArticlesStatus,
    batchRemove,
    batchArticlesTop,
    batchArticlesHot,
    batchArticlesRecommend,
    toggleArticleTop,
    toggleArticleHot,
    toggleArticleRecommend
} from '@/api/crawler'

import { ElMessage } from 'element-plus'

export const useArticleStore = defineStore('crawlerArticle', () => {
    // 状态
    const articleList = ref([])
    const total = ref(0)
    const loading = ref(false)
    const currentPage = ref(1)
    const pageSize = ref(10)
    const searchForm = ref({
        keyword: '',
        source: 'crawler',
        status: -1,
        category: ''
    })
    const crawlerRules = ref([])

    // 获取文章列表
    async function fetchCrawlerArticles() {
        loading.value = true
        try {
            const params = {
                page: currentPage.value,
                size: pageSize.value,
                ...searchForm.value
            }
            const res = await getCrawlerArticles(params)
            articleList.value = res.data.records
            total.value = res.data.total
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    // 更新文章状态
    async function updateStatus(id, status) {
        try {
            await publishCrawlerArticle(id, {status:status})
            ElMessage.success(status === 1 ? '发布成功' : '下架成功')
        } catch (error) {
            console.log('操作失败')
            throw error
        }
    }

    // 切换置顶状态
    async function toggleTop(id, isTop, topOrder) {
        try {
            await toggleArticleTop(id, isTop, topOrder)
            ElMessage.success(isTop ? '置顶成功' : '取消置顶成功')
        } catch (error) {
            console.log('操作失败')
            throw error
        }
    }

    // 切换热门状态
    async function toggleHot(id, isHot, hotOrder) {
        try {
            await toggleArticleHot(id, isHot, hotOrder)
            ElMessage.success(isHot ? '设为热门成功' : '取消热门成功')
        } catch (error) {
            console.log('操作失败')
            throw error
        }
    }

    // 切换推荐状态
    async function toggleRecommend(id, isRecommend, recommendOrder) {
        try {
            await toggleArticleRecommend(id, isRecommend, recommendOrder)
            ElMessage.success(isRecommend ? '设为推荐成功' : '取消推荐成功')
        } catch (error) {
            console.log('操作失败')
            throw error
        }
    }

    // 删除文章
    async function removeArticle(id) {
        try {
            await deleteCrawlerArticle(id)
            ElMessage.success('删除成功')
        } catch (error) {
            console.log('删除失败')
            throw error
        }
    }

    // 批量更新来源
    async function batchUpdateSource(ids, source) {
        try {
            await updateArticlesSource({
                ids:ids,
                source:source
            })
        } catch (error) {
            ElMessage.error('修改来源失败')
            throw error
        }
    }

    // 批量更新状态
    async function batchUpdateStatus(ids, status) {
        try {
            await updateArticlesStatus({
                ids:ids,
                status:status
            })

        } catch (error) {
            ElMessage.error('修改状态失败')
            throw error
        }
    }

    // 批量删除
    async function batchRemoveArticles(ids) {
        try {
            console.log(ids)
            await batchRemove(ids)
        } catch (error) {
            ElMessage.error('删除失败')
            throw error
        }
    }

    // 批量更新置顶状态
    async function updateArticlesTop(ids, isTop, order) {
        try {
            await batchArticlesTop({
                ids:ids,
                isTop:isTop,
                order:order
            })
        } catch (error) {
            ElMessage.error('修改置顶状态失败')
            throw error
        }
    }

    // 批量更新热门状态
    async function updateArticlesHot(ids, isHot, order) {
        try {
            await batchArticlesHot({
                ids:ids,
                isHot:isHot,
                order:order
            })
        } catch (error) {
            ElMessage.error('修改热门状态失败')
            throw error
        }
    }

    // 批量更新推荐状态
    async function updateArticlesRecommend(ids, isRecommend, order) {
        try {
            await batchArticlesRecommend({
                ids:ids,
                isRecommend:isRecommend,
                order:order
            })
        } catch (error) {
            ElMessage.error('修改推荐状态失败')
            throw error
        }
    }


    // 重置搜索条件
    function resetSearch() {
        searchForm.value = {
            keyword: '',
            source: 'crawler',
            status: -1,
            category: ''
        }
    }

    return {
        articleList,
        total,
        loading,
        currentPage,
        pageSize,
        searchForm,
        crawlerRules,
        fetchCrawlerArticles,
        updateStatus,
        toggleTop,
        toggleHot,
        toggleRecommend,
        removeArticle,
        resetSearch,
        batchUpdateSource,
        batchUpdateStatus,
        batchRemoveArticles,
        updateArticlesTop,
        updateArticlesHot,
        updateArticlesRecommend

    }
})

